<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script src="../js/vue.js"></script>
  <body>
    <div id="box">
      <input v-model="val" type="text" />
      <div v-for="(v, index) in list" :key="index">{{v.name}}</div>
      <p v-show="list.length===0">暂无</p>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.0.0-alpha.1/axios.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js"></script>
    <script>
      let arr = [
        {
          name: "zhangsan",
        },
        {
          name: "lisi",
        },
        {
          name: "zhangsi",
        },
        {
          name: "lisan",
        },
      ];
      Mock.mock(RegExp("/api/search" + "*."), (req) => {
        let name = req.url.split("=")[1];
        let filtersArr =arr.filter(item =>item.name.includes(name))
        console.log(filtersArr);
        return filtersArr;
      });
      new Vue({
        el: "#box",
        data: {
            list:'',
          val: "",
        },
        watch: {
          val(name) {
            if(name.length === 0){
                this.list=[]
            }else{
                axios.get("/api/search", {
                params: {
                  name,
                },
              }).then((res) => {
                console.log(res);
                this.list=res.data
              });
              
            }
           
          },
        },
      });
    </script>
  </body>
</html>
